package com.thd.springboottest.security.securitycustom;

import com.thd.springboottest.security.constants.LoginType;
import com.thd.springboottest.security.entity.User;
import com.thd.springboottest.security.service.UserService;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

/**
 * com.thd.springboottest.security.securitycustom.MyProvider
 *
 * @author: wanglei62
 * @DATE: 2022/6/22 15:29
 **/
public class ThirdProvider implements AuthenticationProvider {



    private UserService userService;

    public ThirdProvider(UserService userService){
        this.userService = userService;
    }
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {


        System.out.println("登录认证开始...");

        // 此处可根据token.getLoginType()不同的登录方式  用不同方式验证 ， 如果是微服务验证，建议认证后创建缓存，然后先通过缓存进行验证

        // 这个是未认证的token
        ThirdToken token = (ThirdToken)authentication;
        String userKey = (authentication.getPrincipal() == null) ? "NONE_PROVIDED" : token.getPrincipal().toString();
        String validateCode = (String) authentication.getCredentials();



        String loginType = token.getLoginType();
        User user = null;
        switch (loginType){
            case "MOBILEVALIDATECODE" :
                // 此处填写校验逻辑  此处的123456应该是发送短信验证码时生成的,可以将手机号作为key,生成的验证码作为值存到redis中并设置一个过期时间
                if(!"123456".equals(validateCode)){
                    throw new InternalAuthenticationServiceException("验证码错误");
                }
                // 根据电话号码查询用户信息
                user = userService.loadUserByPhone(userKey);
                break;
            case "USERNAMEPASSWORD" :
                // 此处填写校验逻辑  此处的123456应该是发送短信验证码时生成的,可以将手机号作为key,生成的验证码作为值存到redis中并设置一个过期时间
                if(!"654321".equals(validateCode)){
                    throw new InternalAuthenticationServiceException("验证码错误");
                }

                // 根据用户名查询用户信息
                user = userService.loadUserByAccount(userKey);
                break;
            default:
                if(!"123456".equals(validateCode)){
                    throw new InternalAuthenticationServiceException("验证码错误");
                }

                // 根据电话号码查询用户信息
                user = userService.loadUserByPhone(userKey);
        }



        if(null == user){
            throw new InternalAuthenticationServiceException("未找到该用户");
        }

        // 这个是已认证的token , 这个构造函数中默认setAuthenticated(true)
        ThirdToken result = new ThirdToken(user, validateCode,null);
        result.setDetails(token.getDetails());

        System.out.println("第三方登录认证成功...");
        return result;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        // 如果是第三方登录方式，则在该处理器做登录认证
        return ThirdToken.class.isAssignableFrom(aClass);
    }
}
